﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using SharpDB.Entity;
using SharpDB.SPI;
using SharpDB;
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

namespace SharpDB
{
    /// <summary>
    /// 数据库管理
    /// </summary>
    public class DBMgr
    {
        /// <summary>
        /// 数据库管理
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        public DBMgr(DBType dbType)
            : this(dbType, string.Empty)
        {

        }

        /// <summary>
        /// 数据库管理
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="connectionString">数据库连接字符串</param>
        public DBMgr(DBType dbType, string connectionString)
            : this(dbType, connectionString, true)
        {

        }

        /// <summary>
        /// 数据库管理
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="checkStruct">
        /// 是否开启 表列结构检查(默认开启)
        /// 开启结构检查，则可以避免某些情况的异常。如：给定的列与数据库中的表列不对应 而进行操作。
        /// 关闭结构检查，可以避免因 对表列结构的 检查所造成的性能损失。
        /// </param>
        public DBMgr(DBType dbType, string connectionString, bool checkStruct = true)            
        {
            this.DBType = dbType;

            #region 连接字符串
            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                this._connectionString = connectionString;
            }
            else if (ConfigurationManager.ConnectionStrings[dbType.ToString()] != null)
            {
                this._connectionString = ConfigurationManager.ConnectionStrings[dbType.ToString()].ConnectionString;
            }
            else
            {
                throw new ArgumentNullException("connectionString", dbType.ToString() + "的connectionString不能为空！");
            }

            //处理 如果Sqlite 连接字符串只提供 路径的情况
            if (dbType == DBType.Sqlite
                    && Regex.IsMatch(this._connectionString, @"^(\w):\\(.*)(.+\.db)$",
                    RegexOptions.IgnoreCase | RegexOptions.Compiled))
            {
                this._connectionString = "Data Source=" + this._connectionString;
            }
            #endregion
            
            this.DB = DAOFactory.CreateInstance(DBType, ConnectionString, checkStruct);
        }
        
        /// <summary>
        /// 当前数据库操作(DAO)
        /// </summary>
        public DAOBase DB { get; private set; }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public DBType DBType { get; private set; }

        private string _connectionString = string.Empty;
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_connectionString))
                {
                    if (ConfigurationManager.ConnectionStrings[DBType.ToString()] != null)
                    {
                        this._connectionString = ConfigurationManager.ConnectionStrings[DBType.ToString()].ConnectionString;
                    }
                }
                return _connectionString;
            }
        }

        
    }
}
